/*  GStruct: data structure helpers for Java
 *  Copyright (C) 2011 Giulio Franco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package gstruct.cli;

import java.util.Iterator;

/**
 * A command is an argument that can respond to a set of arguments on its own.
 * @author Giulio Franco <giulio_franco@msn.com>
 *
 * @param <OuterType>	Type of result processed by the argument system this argument is part of.
 * @param <InnerType>	Type of result processed by the argument of this command
 */
public abstract class CLICommand<OuterType, InnerType> extends CLIArgument<OuterType> {
	public static final String ERROR_NOTFIRST =
			"cliarg.error.command.notfirst";
	
	private final CLIArglist<InnerType> arguments;
	
	/**
	 * Creates a CLICommand
	 * @param name	Name of the command
	 * @param description	Description of the command
	 * @param args			Arguments for the command
	 */
	public CLICommand(String name, String description, /*String commandKey,*/
			CLIArgument<? super InnerType>... args) {
		super(name, description, false, true);
		//this.commandKey = commandKey;
		arguments = new CLIArglist<>(args);
	}

	@Override
	public boolean inserted(String matcher, Iterator<String> args,
			ParseResult<? extends OuterType> result) {
		ParseResult<InnerType> res = arguments.parse(
				createResult(), args);
		
		if (!res.isOk()) {
			return false;
		}

		if (!commandInserted(args, result, res)) {
			return false;
		}
		
		return true;
	}

	/**
	 * Factory method for parse results.
	 * This is needed because InnerType is generic.
	 * @return	Fresh result instance
	 */
	protected abstract InnerType createResult();
	
	/**
	 * Called upon insertion of the command,
	 * once all the internal arguments have been processed.
	 * The default implementation always returns true.
	 * Subclasses may override this method, in order to perform
	 * additional coherency checks on the arguments. 
	 * @param args		Iterator pointing to the next argument
	 * @param result	Parse result for the outer command
	 * @param innerResult	Parse result for this command
	 * @return	True iff the parse should continue
	 */
	protected boolean commandInserted(
			Iterator<String> args, 
			ParseResult<? extends OuterType> result,
			ParseResult<InnerType> innerResult) {
		return true;
	}

	@Override
	public boolean isOk(ParseResult<? extends OuterType> result) {
		if (super.isOk(result)) {
			if (result.getArguments().contains(this)) {
				invoke(result);
			}
			return true;
		}
		return false;
	}
	
	/**
	 * Called at the end of the parsing.
	 * This method is supposed to execute the actual command.
	 * @param result	Inner parse result
	 */
	protected void invoke(ParseResult<? extends OuterType> result) { }
}
